Ext.util.Route = Ext.extend(Object, {
    
    
    constructor: function(config) {
        Ext.apply(this, config, {
            conditions: {}
        });
        
        
        this.paramMatchingRegex = new RegExp(/:([0-9A-Za-z\_]*)/g);
        
        // ':target' 得到[':target']
        // ':target:target2' 得到[':target',':target2']
        this.paramsInMatchString = this.url.match(this.paramMatchingRegex) || [];
        
        this.matcherRegex = this.createMatcherRegex(this.url); // url 翻译后的正则表达式对象
    },
    
    // 将url转换成对象
    recognize: function(url) {
        if (this.recognizes(url)) {
            var matches = this.matchesFor(url);
            
            return Ext.applyIf(matches, {
                controller: this.controller,
                action    : this.action,
                historyUrl: url
            });
        }
    },
    
    
    recognizes: function(url) {
        return this.matcherRegex.test(url);
    },
    
    // 将字符串参数转换成对象
    matchesFor: function(url) {
        var params = {},
            keys   = this.paramsInMatchString, // ':target:target2' 得到[':target',':target2']
            values = url.match(this.matcherRegex),
            length = keys.length,
            i;
        
        
        values.shift();

        for (i = 0; i < length; i++) {
            params[keys[i].replace(":", "")] = values[i];
        }

        return params;
    },
    
    
    urlFor: function(config) {
        
    },
    
    // 将自定义格式字符串变成正则表达式对象
    createMatcherRegex: function(url) {
        
        // ':target' 得到[':target']
        // ':target:target2' 得到[':target',':target2']
        var paramsInMatchString = this.paramsInMatchString,
            length = paramsInMatchString.length,
            i, cond, matcher;
        
        // 变量url解析的数组
        for (i = 0; i < length; i++) {
            cond    = this.conditions[paramsInMatchString[i]]; // 获取每个数组对应的条件

            // (表达式)   注：表达式默认[%a-zA-Z0-9\\_\\s,]+
            matcher = Ext.util.Format.format("({0})", cond || "[%a-zA-Z0-9\\_\\s,]+");

            //将url中的 :xxx 替换为 （表达式）
            // 如url是':target:target2'，替换后变成'([%a-zA-Z0-9\_\s,]+):target2'
            url = url.replace(new RegExp(paramsInMatchString[i]), matcher);
        }

        // 最终变成 '^([%a-zA-Z0-9\_\s,]+)([%a-zA-Z0-9\_\s,]+)$'形式
        return new RegExp("^" + url + "$");
    }
});